import os

from django.conf import settings
from django.http import HttpResponseForbidden,JsonResponse
from django.shortcuts import render

# 对接支付宝流程

# 1.登录支付宝 成为开发者

# 2.注册应用 --->appid

# 3.生成 app的公私钥 ---支付宝的公私钥 对接

# 4.使用python-alipy-sdk 装包

# 5.创建aplipay ---

# 6.获取支付的url--支付包网关 + order_string

# 7.用户--扫码支付

# 8.对接支付宝 支付成功的页面回调



from django.views import View

# 1.获取 在支付宝的支付网址 给了浏览器 js 请求
from apps.orders.models import OrderInfo
from utils.response_code import RETCODE


class PaymentView(View):
    def get(self,request,order_id):

        # 1.接收参数 判断order 是否存在
        user = request.user
        try:
            order = OrderInfo.objects.get(order_id=order_id,user=user,status=OrderInfo.ORDER_STATUS_ENUM['UNPAID'])
        except Exception as e:
            return HttpResponseForbidden('订单不存在')

        # 2.创建alipay 对象  链接支付宝
        from alipay import AliPay
        alipay = AliPay(
            appid = settings.ALIPAY_APPID,
            app_notify_url = settings.ALIPAY_RETURN_URL,
            app_private_key_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), "keys/app_private_key.pem"),
            alipay_public_key_path=os.path.join(os.path.dirname(os.path.abspath(__file__)),"keys/alipay_public_key.pem"),
            sign_type="RSA2",
            debug=settings.ALIPAY_DEBUG
        )

        # 3.根据alipay对象 加密参数
        order_string = alipay.api_alipay_trade_page_pay(
            subject = "美多商城%s" % order_id,
            out_trade_no = order_id,
            total_amount = str(order.total_amount),
            return_url=settings.ALIPAY_RETURN_URL,
        )


        # 4.拼接 支付宝的支付url 返回给前端
        # 真实环境电脑网站支付网关：https://openapi.alipay.com/gateway.do? + order_string
        # 沙箱环境电脑网站支付网关：https://openapi.alipaydev.com/gateway.do? + order_string
        alipay_url = settings.ALIPAY_URL + "?" + order_string

        return JsonResponse({'code':RETCODE.OK,'errmsg':'OK','alipay_url':alipay_url})

